package com.sss.demo.dao.impl;

import com.sss.demo.dao.IGoods;
import com.sss.demo.entity.Goods;
import com.sss.demo.util.DBUtil;
import sun.security.pkcs11.Secmod;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class IGoodsImpl implements IGoods {
    Connection conn;
    @Override
    public List<Goods> getAllGoods() throws Exception {
        conn= DBUtil.getConnection();
        String sql="select * from tb_goods";
        PreparedStatement prestat = conn.prepareStatement(sql);
        ResultSet rs = prestat.executeQuery();
        List<Goods> list=new ArrayList<>();
        while (rs.next()){
            Goods goods=new Goods();
            goods.setId(rs.getInt(1));
            goods.setName(rs.getString(2));
            goods.setPrice(rs.getDouble(3));
            goods.setDetail(rs.getString(4));
            list.add(goods);
        }
        return list;
    }

    @Override
    public List<Goods> getGoodsList(int page, int count) throws Exception {
        conn= DBUtil.getConnection();
        String sql="select * from tb_goods limit ?,?";
        PreparedStatement prestat = conn.prepareStatement(sql);
        prestat.setInt(1,(page-1)*5);
        prestat.setInt(2,count);
        ResultSet rs = prestat.executeQuery();
        List<Goods> list=new ArrayList<>();
        while (rs.next()){
            Goods goods=new Goods();
            goods.setId(rs.getInt(1));
            goods.setName(rs.getString(2));
            goods.setPrice(rs.getDouble(3));
            goods.setDetail(rs.getString(4));
            list.add(goods);
        }
        return list;
    }
}
